package cn.chenjifeng.imple;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import cn.chenjifeng.dao.OrderDao;
import cn.chenjifeng.model.Desk_Id_Mdeol;
import cn.chenjifeng.model.OrderModel;
import cn.chenjifeng.model.Vip_id_model;
import cn.common.tool.dele;
import cn.common.tool.seach;

public class OrderImple implements OrderDao
{

	public int AddOrder(OrderModel mo) throws SQLException 
	{
		String sql="insert into pre_order(v_id,t_id,p_time,p_ac_time,p_status) values ( " + 
				  "'"+mo.getV_id()+"',"  +
				  "'"+mo.getT_id()+"'," + 
				  "'"+mo.getP_time()+"'," +
				  "'"+mo.getP_ac_time()+"',"  +
				  "'"+mo.getP_status()+"'" + ")";
		
		String sql_desk = "update desk set t_status= " + 
				  	"'0'"  +" where t_id = "	+
				  	"'"+mo.getT_id()+"'"	;
		
		System.out.println("增加时的sql语句为: "+sql);
		
		dele d = new dele();
		if( d.dele(sql)==1 &&  d.dele(sql_desk)==1 )
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	
	public ArrayList<OrderModel> SearchAll(int page,int pagesum)  throws SQLException
	{
		
		String sql = "SELECT TOP " + pagesum + " * FROM pre_order WHERE pre_order.p_id NOT IN ( SELECT TOP "+page+" pre_order.p_id FROM pre_order)";
		//查pagesum条  从page条开始查
		
		ArrayList<OrderModel> list = new ArrayList<OrderModel>();
		seach b = new seach();
		
		

		ResultSet rs = b.f(sql);
		
		while(rs!=null && rs.next())
		{
			OrderModel order = new OrderModel();
			order.setP_id(rs.getInt("p_id"));
			order.setP_ac_time(rs.getString("p_ac_time"));
			order.setV_id(rs.getString("v_id").trim());
			order.setT_id(rs.getString("t_id").trim());
			order.setP_time(rs.getString("p_time"));
			order.setP_status(rs.getString("p_status").trim());
			
			list.add(order);
		}
		
		return list;
	}
	
	public ArrayList<OrderModel> SearchAll_part(int page,int pagesum,String type,String keyword)  throws SQLException
	{
		
		String sql = "select * from pre_order where "+type+" = '"+keyword+"'";
		
		ArrayList<OrderModel> list = new ArrayList<OrderModel>();
		seach b = new seach();

		ResultSet rs = b.f(sql);
		
		while(rs!=null && rs.next())
		{
			OrderModel order = new OrderModel();
			order.setP_id(rs.getInt("p_id"));
			order.setP_ac_time(rs.getString("p_ac_time"));
			order.setV_id(rs.getString("v_id").trim());
			order.setT_id(rs.getString("t_id").trim());
			order.setP_time(rs.getString("p_time"));
			order.setP_status(rs.getString("p_status").trim());
			
			list.add(order);
		}
		
		return list;
	}


	
	public int total() throws SQLException 
	{
		ResultSet rs;
		seach b = new seach();
		
		String sql_i = "select count(*) from pre_order";
		rs = b.f(sql_i);
		rs.next();
		
		return rs.getInt(1);
		
	}

	
	public ArrayList<Desk_Id_Mdeol> SearchDeakId() throws SQLException 
	{
		String sql = "select t_id from desk where t_status = '1' ";
		
		ArrayList list = new ArrayList<OrderModel>();
		seach b = new seach();

		ResultSet rs = b.f(sql);
		
		while(rs!=null && rs.next())
		{
			Desk_Id_Mdeol desk_id = new Desk_Id_Mdeol();
			desk_id.setD_id(rs.getString("t_id"));
			System.out.println(rs.getString("t_id"));
			
			list.add(desk_id);
		}
		
		return list;
	}


	
	/*查询客户表已存在的id*/
	public ArrayList<Vip_id_model> SearchVipId() throws SQLException 
	{
		String sql = "select v_id from vip";
		
		ArrayList list = new ArrayList<Vip_id_model>();
		seach b = new seach();

		ResultSet rs = b.f(sql);
		
		while(rs!=null && rs.next())
		{
			Vip_id_model vip_id = new Vip_id_model();
			vip_id.setId(rs.getString("v_id"));
			
			list.add(vip_id);
		}
		
		return list;
	}
	
	
	
	public int Alert(OrderModel om) throws SQLException 
	{
		int relax_did,bound_did,updata_pro;
		//1表示餐桌能用   0表示不能用
		//释放原先预定的餐桌  先根据表中原有的id ，查询到桌子id，再到desk表将之释放
		//绑定新的餐桌  再修改预定表
		dele d = new dele();
		
		String sql = "update desk set t_status = 1 where t_id = "
					+"(select t_id from pre_order where p_id = " +
					"'"+om.getP_id()+"')";
		System.out.println("释放餐桌的sql语句为:"+sql);
		relax_did = d.dele(sql);
		
		sql = 	"update pre_order set "				//修改预定表
				+"v_id = " 	 	+ "'"+om.getV_id()+"',"
				+"t_id = " 	 	+ "'"+om.getT_id()+"',"
				+"p_time = " 	+ "'"+om.getP_time()+"',"
				+"p_ac_time = " + "'"+om.getP_ac_time()+"',"
				+"p_status = "  + "'"+om.getP_status()+"'"
				+" where p_id ="+om.getP_id();
		System.out.println("修改预定表的sql语句为:"+sql);
		updata_pro = d.dele(sql);
		
		sql = 	"update desk set "					//绑定新的餐桌
				+"t_status = '0' "
				+" where t_id ='"+om.getT_id()+"'";
		System.out.println("绑定新的餐桌的sql语句为:"+sql);
		bound_did = d.dele(sql);
		
		if(relax_did==1&&bound_did==1&&updata_pro==1)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}


	
	public int Alert_status(OrderModel om) throws SQLException 
	{
		int result1,result2;
		dele d = new dele();
		
		//说明客人已经到店了 1.把对应的餐桌号状态更改  2.把预定表中的状态改为已到店 
		String sql ="update desk set t_status = 1 where t_id = "
					+"'"+om.getT_id()+"'";
		result1 = d.dele(sql);
		System.out.println("修改到店记录的餐桌sql语句为:"+sql);
		
		
		//  
		sql ="update pre_order set p_status = 0 where p_id = "
				+om.getP_id();
		result2 = d.dele(sql);
		System.out.println("修改到店记录的预定表sql语句为:"+sql);
		
		
		if(result1==1&&result2==1)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}



	


}
